<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form
      :model="formData"
      ref="formRef"
      :rules="formRules"
      v-loading="formLoading"
      label-width="100px"
    >
      <el-form-item label="仓库名称" prop="warehouseName">
        <el-input v-model="formData.warehouseName" placeholder="请输入仓库名称" clearable />
      </el-form-item>
      <el-form-item label="仓库负责人" prop="warehouseDirector">
        <el-select v-model="formData.warehouseDirector" placeholder="请输入仓库负责人" clearable>
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.nickname"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="仓库地址" prop="warehouseAddress">
        <el-input
          v-model="formData.warehouseAddress"
          placeholder="请输入仓库地址"
          clearable
          type="textarea"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" clearable type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup name="MaterialWarehouseEdit" lang="ts">
import * as MaterialWarehouseApi from '@/api/material/warehouse'
import * as MaterialWarehouseModel from '@/model/material/info/MaterialWarehouse'
import * as UserApi from '@/api/system/user/user'
const { t } = useI18n()
const message = useMessage()
const dialogTitle = ref('')
const dialogVisible = ref(false)
const formRef = ref()
const formLoading = ref(false)
const formData = ref({
  warehouseName: undefined,
  warehouseDirector: undefined,
  warehouseAddress: undefined,
  remark: undefined
})
let formType = ''
const userList = ref<UserApi.UserVO[]>([]) // 用户列表

const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await MaterialWarehouseApi.singleGet(id)
    } finally {
      formLoading.value = false
    }
  }
  // 获得用户列表
  userList.value = await UserApi.listAllSimple()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    warehouseName: undefined,
    warehouseDirector: undefined,
    warehouseAddress: undefined,
    remark: undefined
  }
  formRef.value?.resetFields()
}

const formRules = reactive({
  warehouseName: [{ required: true, message: '请输入仓库名称', trigger: 'blur' }],
  warehouseDirector: [
    { required: true, type: 'number', message: '请输入仓库负责人', trigger: 'change' }
  ]
})

const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as MaterialWarehouseModel.MaterialWarehouse
    if (formType === 'create') {
      await MaterialWarehouseApi.create(data)
      message.success(t('common.createSuccess'))
    } else {
      await MaterialWarehouseApi.update(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>
